import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router)
//一级路由
import Home from '../views/home.vue';
import Login from '../views/login.vue';
//二级路由


import Messsage from '../views/home/message.vue';


const router = new Router({
    routes:[
        {
            'path':'/login',
            name:'login',
            component:Login,
        },
        {
            'path':'/home',
            name:'home',
            component:Home,
            children:[
                {
                    'path':'message',
                    name:'message',
                    component:Messsage,
                }
            ]
        }
    ]
})


//路由导航首位
router.beforeEach((to, from, next)=>{
    window.console.log(to,from, "router.beforeEach")
    //访问登录页面 直接放行访问(白名单)
    if(to.path == '/login') return next();
    //访问其他路径，判断seesionStorage
    const token = window.sessionStorage.getItem('token');
    if(!token) {
        return next('/login');
    }else {
        if(to.path === '/'){
            next({
                path: '/home/message',
            });
        }
        if (to.path === '/home') {
            next({
                path: '/home/message',
            });
        } else {
            next();
        }
    }
    next();
})


export default router;